package com.cityp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cityp.bean.*;
import com.cityp.mapper.QuizInfoMapper;
import com.cityp.mapper.QuizMapper;
import com.cityp.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "答题记录接口")
@RestController
@RequestMapping("/Quiz")
public class quizReportController {
    @Autowired
    private QuizMapper quizMapper;
    @Autowired
    private QuizInfoMapper quizInfoMapper;
    @ApiOperation(value = "插入答题记录及答题明细")
    @PostMapping("/insertQuiz")
    public ResultBean insertQuiz(@RequestBody @ApiParam("答题记录及明细") Quiz quiz){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        AllUser user = (AllUser) principal;
        quiz.setQuizUserId(user.getUserId());
        quiz.setQuizTime(new UserServiceImpl().getTimeNow());
        quizMapper.insert(quiz);
        long ID=quiz.getQuizId();
        for(QuizInfo info :quiz.getQuizInfoList()){
            info.setExamQuizId(ID);
            info.setExamUserId(user.getUserId());
            quizInfoMapper.insert(info);
        }
        return ResultBean.success("插入成功");
    }
    @ApiOperation("获取答题记录")
    @GetMapping("/getQuiz")
    public ResultBean getQuiz(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        AllUser user = (AllUser) principal;
        List<Quiz> res=quizMapper.selectList(new QueryWrapper<Quiz>().eq("quiz_user_id",user.getUserId()));
        if(res==null){
            return ResultBean.err("查询失败");
        }
        else return ResultBean.success("查询成功",res);
    }
    @ApiOperation("根据答题记录id获取答题明细")
    @GetMapping("/getQuizInfo")
    public ResultBean getQuizInfo(@ApiParam("答题记录的Id") @RequestParam long id){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        AllUser user = (AllUser) principal;
        List<QuizInfo> res=quizInfoMapper.getInfo(user.getUserId(),id);
        if(res==null){
            return ResultBean.err("查询失败");
        }
        else return ResultBean.success("查询成功",res);
    }

}
